import java.io.IOException;
import java.nio.ByteBuffer;

// java -Djava.library.path=. -jar testjar.jar

public class main {

  public static void main(String argv[]) {
    class linkcallback extends evt_callback {
      public linkcallback() {
        super();
      }

      public void run(SWIGTYPE_p_void dat) {
        bb_event_link_state_t link = new bb_event_link_state_t(SWIGTYPE_p_void.getCPtr(dat), false);
        System.out.println("JavaCallback.run() slot = " + link.getSlot() + "prev = " + link.getPrev_state() + "cur = "
            + link.getCur_state());
      }
    }
    System.loadLibrary("ar8030_java");
    System.loadLibrary("ar8030_callback_java");

    bb_host_t host = new bb_host_t("192.168.30.50", 50000);
    bb_dev_list_t lists = new bb_dev_list_t(host);
    int num = lists.get_num();
    System.out.println("num = " + num);

    SWIGTYPE_p_bb_dev_t dev = lists.bb_dev_getlist_index(0);
    SWIGTYPE_p_bb_dev_handle_t hdl = ar8030.bb_dev_open(dev);

    bb_get_status_in_t inst = new bb_get_status_in_t();
    inst.setUser_bmp(0x3fff);
    SWIGTYPE_p_void invoid = new SWIGTYPE_p_void(bb_get_status_in_t.getCPtr(inst), false);

    bb_get_status_out_t outst = new bb_get_status_out_t();
    SWIGTYPE_p_void outvoid = new SWIGTYPE_p_void(bb_get_status_out_t.getCPtr(outst), false);
    int retio = ar8030.bb_ioctl(hdl, ar8030.BB_GET_STATUS, invoid, outvoid);
    int sta = outst.getLink_status().getState();
    System.out.println("sta = " + sta);

    linkcallback linkcb = new linkcallback();
    retio = ar8030_callback.set_cb(hdl, bb_event_e.BB_EVENT_LINK_STATE.swigValue(), linkcb);

    bb_sock_opt_t opt = null;
    int fd = ar8030.bb_socket_open(hdl, bb_slot_e.BB_SLOT_0, 1, 3, opt);
    while (true) {
      java.nio.ByteBuffer buf = ByteBuffer.allocateDirect(1024);
      int ret = ar8030.bb_socket_read(fd, buf, 1024, 1000);
      System.out.println("read len = " + ret);
      for (int i = 0; i < ret; i++) {
        System.out.println("dat = ");
      }
    }
    // ar8030.bb_socket_close(fd);
  }
}
